home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdDictionary.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  4.8 KB  |  198 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  */
  22.  
  23. var gSpellChecker;
  24. var gWordToAdd;
  25.  
  26. function Startup()
  27. {
  28.   if (!InitEditorShell())
  29.     return;
  30.   dump("EditoreditorShell found for dialog\n");
  31.  
  32.   // Get the SpellChecker shell
  33.   gSpellChecker = editorShell.QueryInterface(Components.interfaces.nsIEditorSpellCheck);
  34.   if (!gSpellChecker)
  35.   {
  36.     dump("SpellChecker not found!!!\n");
  37.     window.close();
  38.     return;
  39.   }
  40.   // The word to add word is passed as the 2nd extra parameter in window.openDialog()
  41.   gWordToAdd = window.arguments[1];
  42.   
  43.   gDialog.WordInput = document.getElementById("WordInput");
  44.   gDialog.DictionaryList = document.getElementById("DictionaryList");
  45.   
  46.   gDialog.WordInput.value = gWordToAdd;
  47.   FillDictionaryList();
  48.  
  49.   // Select the supplied word if it is already in the list
  50.   SelectWordToAddInList();
  51.   SetTextboxFocus(gDialog.WordInput);
  52. }
  53.  
  54. function ValidateWordToAdd()
  55. {
  56.   gWordToAdd = TrimString(gDialog.WordInput.value);
  57.   if (gWordToAdd.length > 0)
  58.   {
  59.     return true;
  60.   } else {
  61.     return false;
  62.   }
  63. }    
  64.  
  65. function SelectWordToAddInList()
  66. {
  67.   for (var i = 0; i < gDialog.DictionaryList.getRowCount(); i++)
  68.   {
  69.  
  70.     var wordInList = gDialog.DictionaryList.getItemAtIndex(i);
  71.     if (wordInList && gWordToAdd == wordInList.label)
  72.     {
  73.       gDialog.DictionaryList.selectedIndex = i;
  74.       break;
  75.     }
  76.   }
  77. }
  78.  
  79. function AddWord()
  80. {
  81.   if (ValidateWordToAdd())
  82.   {
  83.     try {
  84.       gSpellChecker.AddWordToDictionary(gWordToAdd);
  85.     }
  86.     catch (e) {
  87.       dump("Exception occured in gSpellChecker.AddWordToDictionary\nWord to add probably already existed\n");
  88.     }
  89.  
  90.     // Rebuild the dialog list
  91.     FillDictionaryList();
  92.  
  93.     SelectWordToAddInList();
  94.     gDialog.WordInput.value = "";
  95.   }
  96. }
  97.  
  98. function ReplaceWord()
  99. {
  100.   if (ValidateWordToAdd())
  101.   {
  102.     var selItem = gDialog.DictionaryList.selectedItem;
  103.     if (selItem)
  104.     {
  105.       try {
  106.         gSpellChecker.RemoveWordFromDictionary(selItem.label);
  107.       } catch (e) {}
  108.  
  109.       try {
  110.         // Add to the dictionary list
  111.         gSpellChecker.AddWordToDictionary(gWordToAdd);
  112.  
  113.         // Just change the text on the selected item
  114.         //  instead of rebuilding the list
  115.         selItem.label = gWordToAdd; 
  116.       } catch (e) {
  117.         // Rebuild list and select the word - it was probably already in the list
  118.         dump("Exception occured adding word in ReplaceWord\n");
  119.         FillDictionaryList();
  120.         SelectWordToAddInList();
  121.       }
  122.     }
  123.   }
  124. }
  125.  
  126. function RemoveWord()
  127. {
  128.   var selIndex = gDialog.DictionaryList.selectedIndex;
  129.   if (selIndex >= 0)
  130.   {
  131.     var word = gDialog.DictionaryList.selectedItem.label;
  132.  
  133.     // Remove word from list
  134.     gDialog.DictionaryList.removeItemAt(selIndex);
  135.  
  136.     // Remove from dictionary
  137.     try {
  138.       //Not working: BUG 43348
  139.       gSpellChecker.RemoveWordFromDictionary(word);
  140.     }
  141.     catch (e)
  142.     {
  143.       dump("Failed to remove word from dictionary\n");
  144.     }
  145.  
  146.     ResetSelectedItem(selIndex);
  147.   }
  148. }
  149.  
  150. function FillDictionaryList()
  151. {
  152.   var selIndex = gDialog.DictionaryList.selectedIndex;
  153.  
  154.   // Clear the current contents of the list
  155.   ClearListbox(gDialog.DictionaryList);
  156.  
  157.   // Get the list from the spell checker
  158.   gSpellChecker.GetPersonalDictionary()
  159.  
  160.   var haveList = false;
  161.  
  162.   // Get words until an empty string is returned
  163.   do {
  164.     var word = gSpellChecker.GetPersonalDictionaryWord();
  165.     if (word != "")
  166.     {
  167.       gDialog.DictionaryList.appendItem(word, "");
  168.       haveList = true;
  169.     }
  170.   } while (word != "");
  171.   
  172.   //XXX: BUG 74467: If list is empty, it doesn't layout to full height correctly
  173.   //     (ignores "rows" attribute) (bug is latered, so we are fixing here for now)
  174.   if (!haveList)
  175.       gDialog.DictionaryList.appendItem("", "");
  176.  
  177.   ResetSelectedItem(selIndex);
  178. }
  179.  
  180. function ResetSelectedItem(index)
  181. {
  182.   var lastIndex = gDialog.DictionaryList.getRowCount() - 1;
  183.   if (index > lastIndex)
  184.     index = lastIndex;
  185.  
  186.   // If we didn't have a selected item, 
  187.   //  set it to the first item
  188.   if (index == -1 && lastIndex >= 0)
  189.     index = 0;
  190.  
  191.   gDialog.DictionaryList.selectedIndex = index;
  192. }
  193.  
  194. function onClose()
  195. {
  196.   return true;
  197. }
  198.